<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      width="30%"
      :close-on-click-modal="false"
    >
      <el-form :model="category">
        <el-form-item label="分类id" v-if="isUpdate">
          <el-input v-model="category.catId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类父级id" >
          <el-input v-model="category.parentCid" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类名" >
          <el-input v-model="category.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitData">确 定</el-button>
      </div>
    </el-dialog>
    <el-input
      placeholder="输入关键字进行过滤"
      v-model="filterText">
    </el-input>
    <el-tree
      :default-expand-all="isExpandAll"
      :data="dataList"
      :props="defaultProps"
      accordion
      show-checkbox
      :filter-node-method="filterNode"
      @node-click="handleNodeClick"
      :expand-on-click-node="false"
      node-key="catId"
      :default-expanded-keys="expandedkeys"
      ref="tree">
       <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
            type="text"
            size="mini"
            v-if="node.level<=2"
            @click="() => append(data)">
            新增
          </el-button>
          <el-button
            type="text"
            size="mini"
            v-if="node.data.catLevel!=1"
            @click="() => remove(node, data)">
            删除
          </el-button>
           <el-button
             type="text"
             size="mini"
             @click="() => edit(node, data)">
            编辑
          </el-button>
        </span>
      </span>
    </el-tree>
  </div>
</template>

<script>
  export default {
    name: "category",
    watch: {
      filterText(val) {//监控关键词的变化
        console.log("val:"+val)
        this.$refs.tree.filter(val);
      }
    },
    created() {
      this.getdata();
    },
    data() {
      return {
        isExpandAll:false,
        isUpdate: false,
        submitType:"",
        title:"",
        expandedkeys:[],
        category:{name:"",parentCid:0,catLevel:0,showStatus:1,sort:0, catId: null},
        dialogFormVisible: false,
        filterText: '',
        dataList: [],
        defaultProps: {
          children: 'childrens',
          label: 'name'
        }
      };
    },
    methods: {
      submitData(){
        if (this.submitType == "add"){
          this.addCategory();
        }else if (this.submitType == "edit"){
          this.updateCategory();
        }
      },
      updateCategory(){
        var { catId, name, icon, productUnit } = this.category;
        this.$http({
          url: this.$http.adornUrl("/product/category/update"),
          method: "post",
          data: this.$http.adornData({ catId, name, icon, productUnit }, false)
        }).then(({ data }) => {
          this.$message({
            message: "修改成功",
            type: "success"
          });
          //关闭对话框
          this.dialogFormVisible = false;
          //刷新出新的菜单
          this.getdata();
          //设置需要默认展开的菜单
          this.expandedkeys = [this.category.parentCid];
        });
      },
      addCategory(){
        this.$http({
          url: this.$http.adornUrl('/product/category/save'),
          method: 'post',
          data: this.$http.adornData(this.category, false)
        }).then(({data}) => {
          if (data.code == 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.getdata()
                //展开当前操作的分类
                this.expandedkeys = [this.category.parentCid]
              }
            })

          } else {
            this.$message.error(data.msg)
          }
          this.dialogFormVisible=false
        })
      },
      //添加
      append(data) {
        console.log("append", data)
        this.submitType = "add";
        this.title = "添加分类";
        this.isUpdate=false;
        this.category.parentCid = data.catId;
        this.category.catLevel = data.catLevel * 1 + 1;
        this.category.catId = null;
        this.category.name = "";
        this.category.icon = "";
        this.category.productUnit = "";
        this.category.sort = 0;
        this.category.showStatus = 1;
        this.dialogFormVisible= true
      },
      edit(node,data) {
        console.log("要修改的数据", data);
        this.submitType = "edit";
        this.title = "修改分类";
        this.isUpdate = true;
        //发送请求获取当前节点最新的数据
        this.$http({
          url: this.$http.adornUrl(`/product/category/info/${data.catId}`),
          method: "get"
        }).then(({ data }) => {
          //请求成功
          console.log("要回显的数据", data);
          this.category.name = data.category.name;
          this.category.catId = data.category.catId;
          this.category.icon = data.category.icon;
          this.category.productUnit = data.category.productUnit;
          this.category.parentCid = data.category.parentCid;
          this.category.catLevel = data.category.catLevel;
          this.category.sort = data.category.sort;
          this.category.showStatus = data.category.showStatus;
        })
        //显示弹窗
        this.dialogFormVisible = true;
      },
      remove(node, data) {
        console.log("remove", node, data)
        //提示用户是否删除
        this.$confirm('是否删除分类' + data.name + '?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {

          this.$http({
            url: this.$http.adornUrl('/product/category/delete'),
            method: 'post',
            data: this.$http.adornData([data.catId], false)
          }).then(({data}) => {
            if (data.code == 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.getdata()
                  //展开当前操作的分类
                  this.expandedkeys = [node.parent.data.catId]
                }
              })

            } else {
              this.$message.error(data.msg)
            }
          })

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });

      },
      filterNode(value, data) {
        if (!value) return true;
        return data.name.indexOf(value) !== -1;
      },
      handleNodeClick(data) {
        console.log("handleNodeClick:" + data);
      },
      getdata() {
        this.$http({
          url: this.$http.adornUrl('/product/category/list/tree'),
          method: 'get',
          params: this.$http.adornParams({},true)
        }).then(({data}) => {
          console.log(data)
          if (data.code == 0) {
            this.dataList = data.data;
          } else {
            this.$message.error(data.msg)
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
